﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Data;

namespace PeisongWeb.admin
{
    public partial class sysAdminEdit : System.Web.UI.Page
    {
        protected int sysId
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["sysId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["sysId"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (logic.sysAdmin.AdminID == 1)
                {
                    if (Request.QueryString["sys"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["sys"]))
                        {
                            sysId = Convert.ToInt32(Request.QueryString["sys"]);
                        }
                    }
                    if (sysId != 0)
                    {
                        getDetails();
                        li_pwd.Visible = li_pwd2.Visible = false;
                    }
                    else
                    {
                        li_pwd.Visible = li_pwd2.Visible = true;
                    }
                    ddlDepartmentBind();
                    ddlSubDepartmentBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }
        //绑定责任部门
        private void ddlDepartmentBind()
        {
            DataTable dt = logic.departmentType.getByParent(0);
            ddl_departmentId.DataSource = dt;
            ddl_departmentId.DataBind();
            //ddl_departmentId.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
        }

        //绑定责任部门——责任分类
        private void ddlSubDepartmentBind()
        {
            if (ddl_departmentId.SelectedValue != "")
            {
                DataTable dt = logic.departmentType.getByParent(Convert.ToInt32(ddl_departmentId.SelectedValue));
                if (dt.Rows.Count!=0)
                {
                    ddl_subDepartmentId.DataSource = dt;
                    ddl_subDepartmentId.DataBind();
                }
                else
                {
                    ddl_subDepartmentId.Items.Clear();
                }
            }
            else
            {
                ddl_subDepartmentId.Items.Clear();
            }
        }

        protected void ddl_departmentId_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlSubDepartmentBind();
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (sysId==0)
            {
                if (logic.sysAdmin.isExistUser(txt_username.Text))
                {
                    Library.Script.ClientMsg("用户名已经存在，重新输入！");
                    return;
                }
            }
            SysAdminEntity adminEnt = new SysAdminEntity();
            adminEnt.Username = txt_username.Text;
            adminEnt.Realname = txt_realname.Text;
            adminEnt.Email = txt_email.Text;
            adminEnt.Mobile = txt_mobile.Text;
            adminEnt.IsSalesperson = cbo_isSalesperson.Checked;
            adminEnt.Disable = cbo_disable.Checked;
            adminEnt.IsAllowAccess = chkAllowAccess.Checked;
            adminEnt.Department = Convert.ToInt32(ddl_subDepartmentId.SelectedValue==""?ddl_departmentId.SelectedValue:ddl_subDepartmentId.SelectedValue);
            if (sysId != 0)
            {
                adminEnt.Password =hid_pwd.Value;
                adminEnt.AdminId = sysId;
                logic.sysAdmin.update(adminEnt);
            }
            else
            {
                adminEnt.Password = Library.StringLbl.EncryptMD5(txt_pwd.Text);
                logic.sysAdmin.insert(adminEnt);
            }
            Library.Script.ClientMsgUrl("保存成功！", "sysAdminList.aspx");
        }
        private void getDetails()
        {
            SysAdminEntity adminEnt = logic.sysAdmin.getById(sysId);
            hid_pwd.Value = adminEnt.Password;
            txt_username.Text = adminEnt.Username;
            txt_username.Enabled = false;
            txt_realname.Text = adminEnt.Realname;
            txt_email.Text = adminEnt.Email;
            txt_mobile.Text = adminEnt.Mobile;
            cbo_isSalesperson.Checked = adminEnt.IsSalesperson;
            cbo_disable.Checked = adminEnt.Disable;
            chkAllowAccess.Checked = adminEnt.IsAllowAccess;
            if (adminEnt.Department!=0)
            {
                DepartmentTypeEntity dtEnt = logic.departmentType.getById(adminEnt.Department);
                if (dtEnt.ParentId == 0)
                {
                    ddl_departmentId.SelectedValue = adminEnt.Department.ToString();
                }
                else
                {
                    ddl_departmentId.SelectedValue = dtEnt.ParentId.ToString();
                    ddl_subDepartmentId.SelectedValue = adminEnt.Department.ToString();
                }
            }
        }
    }
}
